﻿using FlexFlow.Base.Application;
using FlexFlow.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.Runtime.Applications
{
    /// <summary>
    /// 表示分支节点应用程序
    /// </summary>
    public class ForkApplication : AbstractApplication
    {
        public override void OnExecute(ApplicationInvokeContext context, ApplicationExecuteResult result)
        {
            //取得用户提交的表单
            Guid formId = (Guid)context.CurrentProcessInstance.EnterData.FirstOrDefault(m => m.Key == SystemDataSlots.ProcessEnterFormDataSlotID).Value;
            //取得活动设置
            Dictionary<string, string> optDic = context.ActivityOptions.ToDictionary(m => m.Key, m => m.Value);
            //取得节点设置的表单列名称，值，以及比较符
            string colName = optDic["colName"];
            string colValue = optDic["colVal"];
            string oper = optDic["oper"];

            OperateResult getFormResult = WorkFlowFormManager.Default.GetById(formId);
            if (getFormResult.ResultCode != 200)
            {
                throw new Exception("流程进入表单不存在！");
            }
            WorkFlowForm form = getFormResult.Tag as WorkFlowForm;
            string formVal = form.Content[colName] as string;
            string resultString = "未通过";
            switch (oper)
            {
                case "==": 
                    if (formVal == colValue)
                    {
                        //条件满足
                        resultString = "通过";
                    }
                    else
                    {
                        //条件不满足
                        resultString = "未通过";
                    }
                    break;
                case ">":
                    if (double.Parse(formVal)>double.Parse(colValue))
                    {
                        //条件满足
                        resultString = "通过";
                    }
                    else
                    {
                        //条件不满足
                        resultString = "未通过";
                    }
                    break;
                case "<": 
                    if (double.Parse(formVal) < double.Parse(colValue))
                    {
                        //条件满足
                        resultString = "通过";
                    }
                    else
                    {
                        //条件不满足
                        resultString = "未通过";
                    }
                    break;
                case ">=":
                    if (double.Parse(formVal) >= double.Parse(colValue))
                    {
                        //条件满足
                        resultString = "通过";
                    }
                    else
                    {
                        //条件不满足
                        resultString = "未通过";
                    }
                    break;
                case "<=":
                    if (double.Parse(formVal) <= double.Parse(colValue))
                    {
                        //条件满足
                        resultString = "通过";
                    }
                    else
                    {
                        //条件不满足
                        resultString = "未通过";
                    }
                    break;
                case "!=":
                    if (formVal != colValue)
                    {
                        //条件满足
                        resultString = "通过";
                    }
                    else
                    {
                        //条件不满足
                        resultString = "未通过";
                    }
                    break;
                default:
                    throw new Exception("无效比较符：" + oper);
            }
            result.NextActivityIds = this.GetConditionMatchedPath(context.NextPaths, "分支结果", resultString)
                        .Select(m => m.To)
                        .ToArray();
           
        }

        public override void OnTaskSubmit(ApplicationInvokeContext context, WorkFlowTask task, TaskSubmitResult result)
        {
            //分支节点不支持任务提交
            throw new NotImplementedException();
        }
    }
}
